package com.itxiaoxiong.chat.controller;

import com.itxiaoxiong.chat.mapper.SocketMapMapper;
import com.itxiaoxiong.chat.mapper.UserMapper;
import com.itxiaoxiong.chat.pojo.*;
import com.itxiaoxiong.chat.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class UserController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private SocketMapMapper socketMapMapper;
    /*查询整个用户*/
/*
    @GetMapping("/users")
    public Result list(){
        log.info("查询整个用户");

        List<UserTT> userTTList = userService.list();
        return Result.success(userTTList);
    }
*/

    /*注销用户*/
    @DeleteMapping("/users/{account}")
    public Result delete(@PathVariable String account){
        log.info("根据account注销用户: {}", account);
        boolean deleted = userService.delete(account);
        if (deleted) {
            return Result.success();
        } else {
            return Result.error("指定的账号不存在，无法删除");
        }
    }

    /*注册用户*/
    @PostMapping("/users")
    public Result add(@RequestBody User user){
        log.info("新增用户: {}", user);
        String key = "user:" + user.getUsername();
        if (user.getUsername() == null || user.getUsername().isEmpty() ||
                user.getPassword() == null || user.getPassword().isEmpty() ||
                user.getAccount() == null || user.getAccount().isEmpty()) {
            return Result.error("用户名、密码和账户不能为空");
        }
        if (userService.isUsernameExists(user.getUsername())) {
            return Result.error("用户名已存在");
        }
        stringRedisTemplate.opsForValue().set(key, user.getPassword());
        userService.add(user);
        return Result.success();
    }

    /*修改用户名字，密码，头像*/
    @PutMapping("/users")
    public Result update(@RequestBody User user){
        log.info("修改用户: {}", user);
        userService.update(user);
        return Result.success();
    }
    /*
    根据用户名字查询
     */
/*    @GetMapping("/users")
    public Result listUser(@RequestParam String username){
        log.info("根据 username 查询用户:{}", username);
        User user = userService.listUserName(username);
            return Result.success(user);
    }*/

    /*
    根据用户id查询
     */
    @GetMapping("/users")
    public Result listUser(@RequestParam Integer id){
        log.info("根据 id 查询用户:{}", id);
        User user = userService.listUserName(id);
        return Result.success(user);
    }

    /*
    根据id和account进行查询
     */
    @PostMapping("/userAccount")
    public Result accountUser(@RequestBody UserAccount ua){
         User users = userService.userAccount(ua);
         return Result.success(users);
    }

    /*
    查看在线用户
     */
    @GetMapping("/onlineUsers")
    public Result onlineUsers(@RequestParam Integer deleted){
        log.info("根据deleted查询字段:{}", deleted);
        List<String> keys = socketMapMapper.getSocket();
        List<User> onlineUsers = new ArrayList<>();
        for(String key : keys){
            User users = userService.listUserName(Integer.valueOf(key));
            onlineUsers.add(users);
        }
        Map<String, List> result = new HashMap<>();
        result.put("onlineUsers", onlineUsers);
        return Result.success(result);
    }
}
